<template>
  <el-dialog
    :title="`${isCreate ? '添加' : '编辑'}经验等级`"
    :visible.sync="dialogVisible"
    class="dialog"
    width="800px">
    <el-form
      :model="formData"
      label-width="120px"
      ref="form"
      size="small">
      <el-form-item label="积分礼品名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="主图" prop="minEexperience">
        <uploader :src.sync="formData.coverUrl"></uploader>
      </el-form-item>
      <el-form-item label="所需里由卡数" prop="maxEexperience">
        <el-input-number v-model="formData.score" :controls="false"></el-input-number>
      </el-form-item>
      <el-form-item label="详情" prop="maxEexperience">
        <quill-editor
          style="min-height: 180px"
          v-model="formData.giftInfo"
          ref="quillEditor">
        </quill-editor>
      </el-form-item>
    </el-form>
    <dialog-footer
      slot="footer"
      @cancel="dialogVisible = false"
      @confirm="confirm"
    />
  </el-dialog>
</template>
<script>
import { updatePointGift, savePointGift } from '@/api/course';

export default {
  props: {
    initFormData: Object,
    isCreate: Boolean,
  },
  data() {
    return {
      formData: {
        ...this.initFormData,
      },
      dialogVisible: false,
    };
  },
  watch: {
    initFormData(val) {
      this.formData = { ...val };
    },
  },
  methods: {
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const { coverUrl } = this.formData;
          if (coverUrl) {
            if (this.isCreate) {
              await savePointGift(this.formData);
            } else {
              await updatePointGift(this.formData);
            }
            this.dialogVisible = false;
            this.$emit('confirm', this.formData);
          } else {
            this.$message.error('主图不能为空');
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
  .dialog {
    ::v-deep .el-input__inner {
      width: 200px;
    }
  }
</style>
